package cn.jingzhuan.fm.ui.epoxy.model

import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.databinding.ViewFmIndexBannerBinding
import cn.jingzhuan.fm.pojo.ADBanner
import cn.jingzhuan.fm.ui.adapter.BasePagerAdapter
import cn.jingzhuan.fm.utils.ImageLoader
import com.airbnb.epoxy.DataBindingEpoxyModel
import com.airbnb.epoxy.EpoxyModelClass

@EpoxyModelClass(layout = R.layout.view_fm_index_banner)
abstract class FMIndexBannerModel : DataBindingEpoxyModel(){

    var mBannerList: MutableList<ADBanner> = mutableListOf()

    override fun buildView(parent: ViewGroup): View {
        val view = super.buildView(parent)
        var binding = view.tag as ViewFmIndexBannerBinding
        binding.itemBannerPager.adapter = object : BasePagerAdapter<ADBanner>(mBannerList) {
            override fun onBind(data: ADBanner): View {
                val image = ImageView(parent.context)
                ImageLoader.loadImage(image, data.images)
                image.scaleType = ImageView.ScaleType.CENTER_CROP
                image.setOnClickListener {
                    // 跳转专辑详情页面
                }
                return image
            }
        }

        binding.itemBannerIndicator.count = mBannerList.size
        binding.itemBannerPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {}

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                binding.itemBannerIndicator.setSelected(position)
            }
        })
        return view
    }


}
